<?php
abstract class Zend_DBTableAbstract extends Zend_Db_Table_Abstract {
	const ID 				= "id";
	const ORDER_BY 			= "order_by";
	
	/**
	 * Get max value from field in an table
	 * 
	 * @param $tabel string Name of table
	 * @param $field string Name of field
	 * 
	 * @return int Max value from field in table
	 */
	protected function getMaxValue($field, $tabel){
		$sql = "SELECT MAX($field) as `$field` FROM `$tabel`";
		$maxValue = self::singleQuery($sql);
		return $maxValue[$field];
	}
	
	protected function getMaxValueIn($id, $idsField, $fieldNme, $tableName, $additionalWhere = ''){
		$sql = "SELECT MAX($fieldNme) as `$fieldNme` 
				FROM `$tableName`
				WHERE `$idsField` = '$id' $additionalWhere";
		$maxValue = self::singleQuery($sql);
		return $maxValue[$fieldNme];
	}
	
	/**
	 * Get Counts rows in an table
	 * 
	 * @param $tabel Name of table
	 * 
	 * @return int Returns counts rows
	 */
	protected  function getCountRows($tabel){
		$tempFeild = "countRows";
		$sql = "SELECT COUNT(*) as `$tempFeild` FROM `$tabel`";
		$contRows = self::singleQuery($sql);
		return $contRows[$tempFeild];
	}
	
	/**
	 * Return true if name is validate, or false if not
	 *
	 * @param string $name
	 * @param string $field
	 * @param int $withOutId
	 * @return boolean
	 */
	protected function validateName($name, $field, $table, $withoutId=0, $additionalWhere = ''){
		$sql = "SELECT * FROM `$table`
					WHERE 	`$table`.`$field` = '$name' AND 
							`$table`.`".self::ID."` != '$withoutId' $additionalWhere";
		if (self::getNumRows($sql) > 0)
			return false;
		return true;
	}
	
	/**
	 * Get full row from table by id
	 * 
	 * @param $id int Identifier
	 * @param $fieldName string Name of field
	 * @param $tableName string Name of table
	 * 
	 * @return array Returns an associative array that corresponds to the fetched row and moves the internal data pointer ahead
	 */
	protected function getRowBy($id, $fieldName, $tableName, $additionalWhere = ''){
		$sql = "SELECT * FROM `$tableName`
				WHERE `$fieldName` = '$id' $additionalWhere
				LIMIT 1";
		return self::singleQuery($sql);
	}
	
	/**
	 * Update row in table
	 * 
	 * @param $id int Identifier
	 * @param $fieldName string Name of field
	 * @param $tableName string Name of table
	 * @param $values array Associative array
	 * @param $limit int Limit
	 * 
	 * @return boolean Returns status of execute 
	 */
	protected function updateRowBy($id, $fieldName, $tableName, array $values, $limit = 1){
		if (count($values) < 1) return false;
		$limit = ($limit > 0)?"LIMIT $limit":"";
		$array = array();
		foreach ($values as $k => $v)
			$array[] = "`$k` = '$v'";
		$sql = "UPDATE `$tableName` SET
				".join(', ', $array)."
				WHERE  `$fieldName` = '$id'
				$limit";
		return self::query($sql);
	}
	
	/**
	 * Delete row from table
	 * 
	 * @param $id int
	 * @param $tableName string Name of table
	 * @param $values array Associative array
	 * @param $limit int Limit
	 * 
	 * @return boolean Returns status of execute 
	 */
	protected function deleteRowBy($id, $fieldName, $tableName, $limit = 1){
		$limit = ($limit > 0)?"LIMIT $limit":"";
		$sql = "DELETE FROM `$tableName` 
				WHERE `$fieldName` = '$id'
				$limit";
		return self::query($sql);
	}
	
	/**
	 * Inser row in table
	 * 
	 * @param $tableName string Name of table
	 * @param $values array Associative array
	 * 
	 * @return boolean Returns status of execute 
	 */
	protected function insertRow($tableName, array $values){
		if (count($values) < 1) return false;
		$key = array();
		$value = array();
		foreach ($values as $k => $v){
			$key[] = "`$k`";
			$value[] = "'$v'";
		}
		$sql = "INSERT INTO `$tableName` (".join(', ', $key).")
					VALUES(".join(', ', $value).")";
		return self::query($sql);
	}
	
	/**
	 * Get list row
	 * 
	 * @param $sql string A SQL query 
	 * 
	 * @return array Returns array
	 */
	protected function listQuery($sql){
		$check = array();
		$answer = $this->query($sql);
		while ($temp = mysql_fetch_assoc($answer))
			$check[] = $temp;
		return $check;
	}
	
	/**
	 * Get row
	 * 
	 * @param $sql string A SQL query 
	 * 
	 * @return array Returns an associative array that corresponds to the fetched row and moves the internal data pointer ahead
	 */
	protected function singleQuery($sql){
		$answer = $this->query($sql);
		return mysql_fetch_assoc($answer);
	}
	
	/**
	 * Get count rows proper query
	 * 
	 * @param $sql string A SQL query 
	 * 
	 * @return int Returns count rows
	 */
	protected function getNumRows($sql){
		$answer = $this->query($sql);
		return mysql_num_rows($answer);
	}
	
	protected function deleteFromSequenceID($order_id, $fieldName, $tableName, $additionalWhere = ''){
		$sql = "UPDATE `$tableName` 
				SET $fieldName = $fieldName - 1 
				WHERE `$fieldName` > '$order_id' $additionalWhere";
		self::query($sql);
	}
	
	/**
	 * Sends an unique query
	 * 
	 * @param $sql string A SQL query 
	 * 
	 * @return array Returns an associative array that corresponds to the fetched row and moves the internal data pointer ahead
	 */
	protected function query($sql) {
		$answer = mysql_query($sql) or die("$sql Query failed : " . mysql_error());
		return $answer;
	}
	
	protected function moveItem($id, $to, $fieldNameKey, $tableName, $additionalWhere = ''){
		$item = self::getRowBy($id, self::ID, $tableName, $additionalWhere);
		$item2 = self::getRowBy($item[$fieldNameKey]+$to, $fieldNameKey, $tableName, $additionalWhere);
		
		if (isset($item2[self::ID])){
			self::updateRowBy($item[self::ID], self::ID, $tableName, array($fieldNameKey => $item2[$fieldNameKey]),1);
			self::updateRowBy($item2[self::ID], self::ID, $tableName, array($fieldNameKey => $item[$fieldNameKey]),1);
			return true;
		}
		return false;
	}
}
?>